import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Student from "../views/Student.vue"
import Login from "../views/Login.vue"
import Fraction from "../views/Fraction.vue"

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home,
    // 重定向路径，访问/时会自动跳转到student页面
    redirect:'/student'
  },
  {
    path: '/student',
    name: 'Student',
    component: Student
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/fraction',
    name: 'Fraction',
    component: Fraction
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path != "/login") {
    if (localStorage.getItem("usr")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
})

const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

export default router